Uec3.RequestAddPartnerWindow = Ext.extend(Ext.app.Module, {
	id : 'requestAddPartner',
	type : 'common/requestAddPartner',

	/**
	 * Initialize this module. This function is called at startup (page
	 * load/refresh).
	 */
	init : function(initParams) {
	},
	lookUpMessage : function(id) {						   						   
		var desktop = Uec3.App.getDesktop();
		desktop.launchWindowByParam("simpleCompanyMessage", {
			        partnerId : '',
					id : id,
					isHiddenButton:true
				});	 
},
	getComponents : function(params) {

		var requestAddPartnerForm;
		var requestAddPartner_ajax;
      
		var componentRequestAddPartnerInits = function() {

			requestAddPartner_ajax = new Ext.data.Connection();		
			function getText(companyId,name,text){
			    if(!text){ text="公司,加为您的往来单位";}			
				var str = "<div style='font:normal 12px sans-serif'>您请求<a style='font-size:12px;' href='javascript:Ext.CUR_APP.getModule(\"requestAddPartner\").lookUpMessage("
					+ companyId+");'>"
					+name
					+"</a>"+
					text
					+"</div>"
				
				return str;
			}		
			requestAddPartner_storeRelation = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
					url : Uec3.App.Constants.homeUrl
							+ '/dictionary/findByVarGroup.do?group=partner_relation'
				}),
				reader : new Ext.data.JsonReader({
							root : '',
							fields : [{
										name : 'varIndex'
									}, {
										name : 'varName'
									}]
						})
			});
			
			requestAddPartner_comboRelation = new Ext.form.ComboBox({
				hiddenName : 'relation',
				fieldLabel : '请求对方成为您的',
				store : requestAddPartner_storeRelation,
				forceSelection : true,
				emptyText : '--请选择--',
				mode : 'local',
				width : 100,
				triggerAction : 'all',
				valueField : 'varIndex',
				displayField : 'varName',
				loadingText : '加载中...',
				editable : false,
				allowBlank : false,
				blankText : "往来关系不能为空"

			})
			
			requestAddPartner_storeRelation.load(			
			{callback:function(){			
				if(params.isAlertOrder){
				var record = requestAddPartner_comboRelation.findRecord(requestAddPartner_comboRelation.valueField,1);	
				if(record){
				  var text=record.get(requestAddPartner_comboRelation.displayField);
				  requestAddPartner_comboRelation.setRawValue(text);	
				  requestAddPartner_comboRelation.setValue(1);
				  requestAddPartner_comboRelation.setReadOnly(true);
				}
			  }}
			 }
			);
			requestAddPartnerForm = new Ext.form.FormPanel({
				labelAlign : 'right',
				frame : true,
				labelWidth :65,
				height : 300,
				loadingText : '加载数据中...',
				buttonAlign : 'right',
				layout : "form",
				items : [{			
							xtype : 'panel',
							html: getText(params.companyId,params.name,params.text)
						}, 
						{xtype:'panel',
						 anchor : '99%',
						 fieldLabel : '将对方加为',
						 items : [requestAddPartner_comboRelation]
						},{							     
				        	 xtype:'label',
							 text:'请您输入留言:',
							 cls:'x-form-field',
							 anchor : '99%'
				        },{
							        	height:120,
							        	hideLabel : true,
										name : 'reason',
										preventScrollbars : true,
										xtype : 'textarea',
										anchor : '99%',
										emptyText : '',
										maxLength:50,
										maxLengthText:"您的留言不能超过50个字符"						
						}
				],
				buttons : [{
					text : '发送请求',
					handler : function() {
						if (!requestAddPartnerForm.getForm().isValid()) {
							Ext.Msg.alert("提示", "请填写必须项");
							return;
						}
						if (params.companyId == 0 || params.companyId== "") {
							Ext.Msg.alert("提示","请先点击检查认证按钮,确认对方已是认证公司,才能进行这步操作")
							return;
						}						
						var relation = requestAddPartnerForm.getForm().findField("relation").getValue();	   
						var reason = requestAddPartnerForm.getForm().findField("reason").getValue();
						requestAddPartner_ajax.request({
							url : Uec3.App.Constants.homeUrl
									+ '/partner/query_partner!sendNotice.do',
							success : function(res) {
								var result = Ext.decode(res.responseText);
								if (result.success == "OK") {
								  Ext.Msg.alert("提示", "发送消息成功",function(){requestAddPartnerForm.ownerCt.close();});
								} else {Ext.Msg.alert("提示", result.failure);}
							},
							failure : function(res) {Ext.Msg.alert('失败', "网络出现异常,请重先连接");},
							params : {receiverId : params.companyId,relation:relation,reason:reason,partnerId:params.partnerId}
						});
					}
				}, {
					text : '关闭',
					handler : function() {requestAddPartnerForm.ownerCt.close();},
					style : "margin-left:20px"
				}]
			});
			

		};

		var controlRequestAddPartnerEvent = function() {

			requestAddPartner_ajax.on('beforerequest', function() {Ext.MessageBox.wait("更新数据中...", "提示");});

			requestAddPartner_ajax.on('requestcomplete', function() {Ext.MessageBox.hide();});

		};

		componentRequestAddPartnerInits();

		controlRequestAddPartnerEvent();

		return requestAddPartnerForm;

	},

	/**
	 * Create this modules window here.
	 */
	createWindow : function(params){
		var windowId = 'requestAddPartner';
		var desktop = this.app.getDesktop();
		if (params) {windowId += "_" + params.winId;}	
		var win = desktop.getWindow(windowId);
		if (!win) {
			win = desktop.createWindowNoTaskMaxZIndex({
						id : windowId,
						title : '请求加为往来单位',
						width : 400,
						height : 300,
						iconCls : 'acc-icon',
						shim : false,
						modal:true,
						animCollapse : false,
						constrainHeader : true,
						resizable : false,
						maximizable : false,
						taskbuttonTooltip : "请求加为往来单位",
						layout : "fit",
						tools : [{id : 'refresh',handler : Ext.emptyFn,scope : this}],
						items : [this.getComponents(params)]
					});

		}
		win.show();
	}
});